import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
class Base():
    def __init__(self,driver):
        self.driver=driver
    #封装浏览器方法
    def open_url(self,url):
        self.driver.get(url)

    #封装元素定位的方法和显示等待10秒每0.5秒执行一次
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,10,0.5).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            t=time.strftime('%Y%m%d-%H%M%S',time.localtime())
            self.driver.save_screenshot(t+'.png')
            print(e)
    #封装点击方法
    def click(self,loc):
        self.find_element(loc).click()
    #封装输入方法
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 封装获取文本方法
    def text(self,loc):
        return self.find_element(loc).text
    # 封装滚动条方法
    def scroll(self,x):
        self.driver.execute_script(f'window_scrollTo(0,{x})')
    # 封装弹框处理方法
    def alert(self):
        self.driver.switch_to.alert.accept()
    # 封装iframe处理方法
    def iframe(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 封装切换页面方法
    def window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 封装鼠标事件方法
    def actionchains(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    # 封装键盘事件方法
    def keys(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # 封装浏览器事件方法
    def refresh(self):
        self.driver.refresh()
    # 封装下拉框选择方法
    def select(self,loc):
        Select(self.find_element(loc)).select_by_index(1)